package com.fary.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.fary.bo.NotifyMerchantBO;
import com.fary.buffer.MqBatchDeliveryBuffer;
import com.fary.constant.TemplateMessageConstant;
import com.fary.entity.PayMerchantInfo;
import com.fary.service.PayMessageTemplateService;
import com.fary.service.WeChatWxMpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.fary.constant.RabbitMQExchangeConstant.WECHAT_TEMPLATE_EXCHANGE;


@Service
@Slf4j
public class PayMessageTemplateImpl implements PayMessageTemplateService {

    @Autowired
    private WeChatWxMpService weChatWxMpService;

    /**
     * 用户支付成功之后推送商家提醒的模板id
     */
    @Value("${mayikt.pay.notifyMerchantTemplateId}")
    private String notifyMerchantTemplateId;
    /**
     * 商家提交资料模板id
     */
    @Value("${mayikt.pay.merchantSettlementId}")
    private String merchantSettlementId;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MqBatchDeliveryBuffer<NotifyMerchantBO> mqBatchDeliveryBuffer;

    public boolean notifyMerchantPaymentResult(NotifyMerchantBO notifyMerchantBO) {
        Map<String, String> keywordMap = new HashMap<>();
        keywordMap.put(TemplateMessageConstant.TEMPLATE_ID, notifyMerchantTemplateId);
        keywordMap.put(TemplateMessageConstant.TO_USER, notifyMerchantBO.getMerchantOpenId());
        keywordMap.put(TemplateMessageConstant.URL, "www.baidu.com");
        keywordMap.put("first", notifyMerchantBO.getMerchantId());
        keywordMap.put("keyword1", DateUtil.formatDateTime(notifyMerchantBO.getPayDate()));
        keywordMap.put("keyword2", notifyMerchantBO.getPaymentChannel());
        keywordMap.put("keyword3", notifyMerchantBO.getPaymentAmount().toString());
        keywordMap.put("keyword4", notifyMerchantBO.getPaymentId());
        return weChatWxMpService.sendTemplateMsg(keywordMap);
    }

    @Async("taskExecutor")
    public void notifyMerchantPaymentResultThread(NotifyMerchantBO notifyMerchantBO) {
        Map<String, String> keywordMap = new HashMap<>();
        keywordMap.put(TemplateMessageConstant.TEMPLATE_ID, notifyMerchantTemplateId);
        keywordMap.put(TemplateMessageConstant.TO_USER, notifyMerchantBO.getMerchantOpenId());
        keywordMap.put(TemplateMessageConstant.URL, "www.baidu.com");
        keywordMap.put("first", notifyMerchantBO.getMerchantId());
        keywordMap.put("keyword1", DateUtil.formatDateTime(notifyMerchantBO.getPayDate()));
        keywordMap.put("keyword2", notifyMerchantBO.getPaymentChannel());
        keywordMap.put("keyword3", notifyMerchantBO.getPaymentAmount().toString());
        keywordMap.put("keyword4", notifyMerchantBO.getPaymentId());
        weChatWxMpService.sendTemplateMsg(keywordMap);
    }

    public void notifyMerchantPaymentResultMQ(NotifyMerchantBO notifyMerchantBO) {
        String msg = JSONUtil.toJsonStr(notifyMerchantBO);
        // 先记录日志msg 写入redis 或者是mysql中
        rabbitTemplate.convertAndSend(WECHAT_TEMPLATE_EXCHANGE, "", msg);
        // 人工干预
        log.info("异步发送商户端支付结果投递消息提醒msg:{}", msg);
    }

    @Override
    public void notifyMerchantPaymentResultBatchMQ(NotifyMerchantBO notifyMerchantBO) {
        // 该代码为测试代码 并非正式代码
        for (int i = 1; i <= 10; i++) {
            NotifyMerchantBO newNotifyMerchant = BeanUtil.copyProperties(notifyMerchantBO, NotifyMerchantBO.class);
            newNotifyMerchant.setPaymentId("1111");
            mqBatchDeliveryBuffer.add(newNotifyMerchant);
        }
    }

    /**
     * 商家入驻提交资料通知
     */
    @Async("taskExecutor")
    public void notifyMerchantSettlementThread(PayMerchantInfo payMerchantInfo) {
        Map<String, String> keywordMap = new HashMap<>();
        keywordMap.put(TemplateMessageConstant.TEMPLATE_ID, merchantSettlementId);
        keywordMap.put(TemplateMessageConstant.TO_USER, payMerchantInfo.getContactWxOpenid());
        keywordMap.put(TemplateMessageConstant.URL, "www.baidu.com");
        keywordMap.put("first", payMerchantInfo.getEnterpriseName());
        weChatWxMpService.sendTemplateMsg(keywordMap);
    }
}

